<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="设备编码">
              <a-input placeholder="请输入设备编码" v-model="queryParam.code"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="设备名称">
              <a-input placeholder="请输入设备名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="设备型号">
                <a-input placeholder="请输入设备型号" v-model="queryParam.mode"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="设备类型">
                <!--<a-input placeholder="请输入设备类型" v-model="queryParam.categoryId"></a-input>-->
                <a-select v-model="queryParam.categoryId">
                  <a-select-option :key="item.id" v-for="item in categoryList" :value="item.id">{{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="设备状态">
                <j-dict-select-tag placeholder="请选择设备状态" v-model="queryParam.status" dictCode="equipment_status"
                                   style="width: 200px"/>
                <!--<a-input placeholder="请输入设备状态" v-model="queryParam.status"></a-input>-->
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="检定单位">
                <a-input placeholder="请输入检定单位" v-model="queryParam.verificationUnit"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="单位名称">
                <a-input placeholder="请输入单位名称" v-model="queryParam.unitName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="负责人">
                <a-input placeholder="请输入负责人" v-model="queryParam.responsible"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="10" :lg="11" :md="12" :sm="24">
              <a-form-item label="有效期">
                <!--<j-date placeholder="请选择开始日期" class="query-group-cust" v-model="queryParam.validityDate_begin"></j-date>-->
                <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="queryParam.dateBegin"></j-date>
                <span class="query-group-split-cust"></span>
                <!--<j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.validityDate_end"></j-date>-->
                <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.dateEnd"></j-date>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('equipment')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete"/>
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
        selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>


      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <!--设备状态样式渲染-->
        <template slot="statusStr" slot-scope="text,record,index">
          <a-tag v-if="record.status === '-2'" color="red">
            故障
          </a-tag>
          <a-tag v-else-if="record.status === '-1'" color="gray">
            停用
          </a-tag>
          <a-tag v-else-if="record.status === '0'" color="blue">
            空闲
          </a-tag>
          <a-tag v-else-if="record.status === '1'" color="orange">
            维修
          </a-tag>
          <a-tag v-else-if="record.status === '2'" color="green">
            运行
          </a-tag>
          <a-tag v-else color="black">
            未知
          </a-tag>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>

              <a-menu-item>
                  <a @click="handleShow(record)">查看维修记录</a>
              </a-menu-item>

              <a-menu-item>
                  <a @click="showVerification(record)">查看检出记录</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <equipment-modal ref="modalForm" @ok="modalFormOk"></equipment-modal>

    <!--维修记录页面区域-->
    <a-modal
      title="查看维修信息"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      width="850px">
      <!--table区域-->
      <a-table :columns="columns1" :data-source="data" style="margin-top: 20px"  bordered
               rowKey="id" >
        <a slot="name" slot-scope="text">{{ text }}</a>
      </a-table>
    </a-modal>

    <!--检定记录页面区域-->
    <a-modal v-model="visibleVerification" title="鉴定记录信息" @ok="closeVerification" @cancel="closeVerification">
      <a-form-model :model="verification" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-form-model-item label="设备编码">
          <a-input v-model="verification.equipmentCode" disabled/>
        </a-form-model-item>

        <a-form-model-item label="检定部门">
          <a-input v-model="verification.department" disabled/>
        </a-form-model-item>

        <a-form-model-item label="检定人">
          <a-input v-model="verification.person" disabled/>
        </a-form-model-item>

        <a-form-model-item label="检定起始时间">
          <j-date v-model="verification.startTime" style="width: 100%" disabled/>
        </a-form-model-item>

        <a-form-model-item label="检定结束时间">
          <j-date v-model="verification.endTime" style="width: 100%" disabled/>
        </a-form-model-item>

        <a-form-model-item label="检定结果">
          <a-input v-model="verification.result" disabled/>
        </a-form-model-item>

        <a-form-model-item label="备注">
          <a-input v-model="verification.description" disabled/>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import {mixinDevice} from '@/utils/mixin'
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import EquipmentModal from './modules/EquipmentModal'
  import JDate from '@/components/jeecg/JDate.vue'

  import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  // 引入JS
  import {filterDictTextByCache} from '@/components/dict/JDictSelectUtil'
  import {getAction} from "../../../api/manage";

  export default {
    name: 'EquipmentList',
    mixins: [JeecgListMixin, mixinDevice],
    components: {
      JDictSelectTag,
      JDate,
      EquipmentModal,
    },
    data() {
      return {
        //设备检出记录实体
        verification: {
          equipmentCode: "",
          department: "",
          person: "",
          startTime: "",
          endTime: "",
          result: "",
          description: "",
        },
        //检出记录弹窗时的参数
        visibleVerification: false,//控制对话框显示隐藏属性
        labelCol: {span: 6},
        wrapperCol: {span: 18},
        //对话框
        visible: false,
        confirmLoading: false,
        description: 'equipment管理页面',
        //设备类型列表
        categoryList: [],
        data: [],
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: "center",
            customRender: function (t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '设备编码',
            align: "center",
            dataIndex: 'code'
          },
          {
            title: '设备名称',
            align: "center",
            dataIndex: 'name'
          },
          {
            title: '设备型号',
            align: "center",
            dataIndex: 'mode'
          },
          {
            title: '设备类型',
            align: "center",
            dataIndex: 'categoryName'
          },
          {
            title: '设备状态',
            align: "center",
            dataIndex: 'status',
            scopedSlots: {customRender: 'statusStr'}
          },
          {
            title: '检定单位',
            align: "center",
            dataIndex: 'verificationUnit'
          },
          {
            title: '单位名称',
            align: "center",
            dataIndex: 'unitName'
          },
          {
            title: '负责人',
            align: "center",
            dataIndex: 'responsible'
          },
          {
            title: '有效期',
            align: "center",
            dataIndex: 'validityDate',
            customRender: function (text) {
              return !text ? "" : (text.length > 10 ? text.substr(0, 10) : text)
            }
          },
          {
            title: '描述',
            align: "center",
            dataIndex: 'description'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: "center",
            fixed: "right",
            width: 147,
            scopedSlots: {customRender: 'action'}
          }
        ],
        columns1: [
          {
            title: '序号',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: "center",
            customRender: function (t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title:'设备编码',
            align:"center",
            dataIndex: 'equipmentCode'
          },
          {
            title:'维修部门',
            align:"center",
            dataIndex: 'department'
          },
          {
            title:'机修人员',
            align:"center",
            dataIndex: 'maintainPerson'
          },
          {
            title: '检查人',
            align: "center",
            dataIndex: 'checkPerson'
          },
          {
            title:'电工人员',
            align:"center",
            dataIndex: 'electricianPerson'
          },
          {
            title:'检查人',
            align:"center",
            dataIndex: 'checkPerson'
          },
          {
            title:'电工人员',
            align:"center",
            dataIndex: 'electricianPerson'
          },
          {
            title:'维修起始时间',
            align:"center",
            dataIndex: 'repairStartTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'维修结束时间',
            align:"center",
            dataIndex: 'repairEndTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'维修原因',
            align:"center",
            dataIndex: 'reason'
          },
          {
            title:'维修结果',
            align:"center",
            dataIndex: 'result'
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'description'
          },
        ],
        url: {
          getVerification:"/verification/equipmentVerification/queryByCode",
          getType:"/equipment/equipment/getEquipmentType",
          list: "/equipment/equipment/list",
          delete: "/equipment/equipment/delete",
          deleteBatch: "/equipment/equipment/deleteBatch",
          exportXlsUrl: "/equipment/equipment/exportXls",
          importExcelUrl: "equipment/equipment/importExcel",
          queryRepair:"/equipment/equipment/queryRepair"

        },
        dictOptions:{},
      }
    },
    created() {
      this.getCategoryList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      //显示检出记录
      showVerification(record) {
        if (record.code) {
          getAction(this.url.getVerification, {code: record.code}).then(
            res => {
              if (res.success) {
                console.log(res);
                this.verification=res.result;
                this.visibleVerification=true;
              } else {
                alert(res.message);
              }
            }
          )
        }
      },
      //关闭查看检出记录弹窗
      closeVerification() {
        this.visibleVerification = false;
        this.verification.equipmentCode = '';
        this.verification.department = '';
        this.verification.person = '';
        this.verification.startTime = '';
        this.verification.endTime = '';
        this.verification.result = '';
        this.verification.description = '';
      },
      initDictConfig() {
      },
      getCategoryList() {
        getAction(this.url.getType).then(res => {
          if (res.success) {
            //console.log(res);
            this.categoryList=res.result;
          }
        })
      },
      //维修记录弹框
      handleShow(record) {
        getAction(this.url.queryRepair,{code:record.code}).then(res =>{
          if(res.result!=null&&res.result.length!=0)
          {
            this.data=res.result;
            this.visible = true;
          }
          else
          {
            alert("没有维修记录");
          }
        })
      },
      //新增信息
      handleOk(e) {
        this.visible=false;
          },
      //取消
      handleCancel(e) {
        this.visible = false;
      },
    },
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>